import java.util.List;
import java.util.Scanner;

public class ScheduleService {
    private Scanner scanner;

    public ScheduleService(Scanner scanner) {
        this.scanner = scanner;
    }

    // 添加场次
    public boolean addSchedule() {
        System.out.print("请输入场次ID: ");
        String id = scanner.nextLine();

        // 检查场次ID是否已存在
        if (DataAccess.findScheduleById(id) != null) {
            System.out.println("场次ID已存在！");
            return false;
        }

        System.out.print("请输入影片ID: ");
        String movieId = scanner.nextLine();

        // 检查影片是否存在
        if (DataAccess.findMovieById(movieId) == null) {
            System.out.println("影片不存在！");
            return false;
        }

        // 显示可选放映厅
        System.out.println("可选放映厅:");
        for (int i = 0; i < DataAccess.HALLS.length; i++) {
            System.out.println((i + 1) + ". " + DataAccess.HALLS[i] + " (" + DataAccess.HALL_SEATS[i] + "座)");
        }
        System.out.print("请选择放映厅 (输入序号): ");
        int hallIndex = 0;
        try {
            hallIndex = Integer.parseInt(scanner.nextLine()) - 1;
            if (hallIndex < 0 || hallIndex >= DataAccess.HALLS.length) {
                System.out.println("放映厅选择错误！");
                return false;
            }
        } catch (NumberFormatException e) {
            System.out.println("输入错误！");
            return false;
        }
        String hall = DataAccess.HALLS[hallIndex];
        int seatCount = DataAccess.HALL_SEATS[hallIndex];

        System.out.print("请输入放映日期 (如2023-10-01): ");
        String date = scanner.nextLine();
        System.out.print("请输入放映时间 (如19:00): ");
        String time = scanner.nextLine();

        Schedule newSchedule = new Schedule(id, movieId, hall, date, time, seatCount);
        if (DataAccess.addSchedule(newSchedule)) {
            System.out.println("场次添加成功！");
            return true;
        } else {
            System.out.println("场次添加失败！");
            return false;
        }
    }

    // 修改场次信息
    public boolean updateSchedule() {
        System.out.print("请输入要修改的场次ID: ");
        String id = scanner.nextLine();

        Schedule schedule = DataAccess.findScheduleById(id);
        if (schedule == null) {
            System.out.println("场次不存在！");
            return false;
        }

        System.out.println("当前场次信息:");
        System.out.println("场次ID: " + schedule.getId() + ", 影片ID: " + schedule.getMovieId() +
                ", 放映厅: " + schedule.getHall() + ", 日期: " + schedule.getDate() +
                ", 时间: " + schedule.getTime());

        System.out.print("请输入新的影片ID (不修改按回车): ");
        String movieId = scanner.nextLine();
        if (!movieId.isEmpty()) {
            // 检查新影片是否存在
            if (DataAccess.findMovieById(movieId) == null) {
                System.out.println("影片不存在，未更新影片ID！");
            } else {
                schedule.setMovieId(movieId);
            }
        }

        System.out.print("请输入新的放映日期 (不修改按回车): ");
        String date = scanner.nextLine();
        if (!date.isEmpty()) {
            schedule.setDate(date);
        }

        System.out.print("请输入新的放映时间 (不修改按回车): ");
        String time = scanner.nextLine();
        if (!time.isEmpty()) {
            schedule.setTime(time);
        }

        DataAccess.updateSchedule(schedule);
        System.out.println("场次信息更新成功！");
        return true;
    }

    // 删除场次
    public boolean deleteSchedule() {
        System.out.print("请输入要删除的场次ID: ");
        String id = scanner.nextLine();

        if (DataAccess.deleteSchedule(id)) {
            System.out.println("场次已删除！");
            return true;
        } else {
            System.out.println("删除失败，场次不存在！");
            return false;
        }
    }

    // 列出所有场次
    public void listAllSchedules() {
        List<Schedule> schedules = DataAccess.getAllSchedules();
        System.out.println("\n=== 场次列表 ===");
        System.out.println("场次ID\t影片ID\t放映厅\t日期\t\t时间\t可售座位");

        for (Schedule schedule : schedules) {
            Movie movie = DataAccess.findMovieById(schedule.getMovieId());
            String movieName = (movie != null) ? movie.getName() : "未知影片";

            System.out.println(schedule.getId() + "\t" + schedule.getMovieId() +
                    "(" + movieName + ")\t" + schedule.getHall() + "\t" +
                    schedule.getDate() + "\t" + schedule.getTime() + "\t" +
                    schedule.getAvailableSeatsCount());
        }
    }

    // 按影片ID列出场次
    public void listSchedulesByMovieId(String movieId) {
        List<Schedule> schedules = DataAccess.getSchedulesByMovieId(movieId);
        if (schedules.size() == 0) {
            System.out.println("该影片暂无排片！");
            return;
        }

        Movie movie = DataAccess.findMovieById(movieId);
        String movieName = (movie != null) ? movie.getName() : "未知影片";

        System.out.println("\n=== " + movieName + " 排片列表 ===");
        System.out.println("场次ID\t放映厅\t日期\t\t时间\t可售座位");

        for (Schedule schedule : schedules) {
            System.out.println(schedule.getId() + "\t" + schedule.getHall() + "\t" +
                    schedule.getDate() + "\t" + schedule.getTime() + "\t" +
                    schedule.getAvailableSeatsCount());
        }
    }
}
